<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  
  <title>sed 命令从入门到精通 | 四叶草</title>
  
  <meta name="keywords" content="archlinux,linux">
  
  
  <meta name="description" content="一个技术宅的技术分享">
  

  
  <link rel="alternate" href="/atom.xml" title="四叶草">
  

  <meta name="HandheldFriendly" content="True" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <!-- meta -->
  

  <!-- link -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.10.1/css/all.min.css">
  

  

  
    
<link rel="stylesheet" href="/style.css">

  

  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>

  
  
<meta name="generator" content="Hexo 4.2.1"><link rel="alternate" href="/atom.xml" title="四叶草" type="application/atom+xml">
</head>

<body>
  
  
  <header class="l_header material">
  <div id="loading-bar-wrapper">
    <div id="loading-bar" class="material"></div>
  </div>

	<div class='wrapper'>
		<div class="nav-main container container--flex">
      <a class="logo flat-box" href='/' >
        
          四叶草
        
      </a>
			<div class='menu navgation'>
				<ul class='h-list'>
          
  					
  						<li>
								<a class="nav flat-box" href="/"
                  
                  
                  id="home">
									<i class='fas fa-home fa-fw'></i>&nbsp;主页
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/categories/"
                  
                    rel="nofollow"
                  
                  
                  id="categories">
									<i class='fas fa-folder-open fa-fw'></i>&nbsp;分类
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/tags/"
                  
                    rel="nofollow"
                  
                  
                  id="tags">
									<i class='fas fa-hashtag fa-fw'></i>&nbsp;标签
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/archives/"
                  
                    rel="nofollow"
                  
                  
                  id="archives">
									<i class='fas fa-archive fa-fw'></i>&nbsp;归档
								</a>
							</li>
      			
      		
				</ul>
			</div>

			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="搜索" />
						<i class="icon fas fa-search fa-fw"></i>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a class="fas fa-search fa-fw" href='javascript:void(0)'></a></li>
				
				<li class='s-menu'><a class="fas fa-bars fa-fw" href='javascript:void(0)'></a></li>
			</ul>
		</div>

		<div class='nav-sub container container--flex'>
			<a class="logo flat-box"></a>
			<ul class='switcher h-list'>
				<li class='s-comment'><a class="flat-btn fas fa-comments fa-fw" href='javascript:void(0)'></a></li>
        
          <li class='s-toc'><a class="flat-btn fas fa-list fa-fw" href='javascript:void(0)'></a></li>
        
			</ul>
		</div>
	</div>
</header>
	<aside class="menu-phone">
    <header>
		<nav class="menu navgation">
      <ul>
        
          
            <li>
							<a class="nav flat-box" href="/"
                
                
                id="home">
								<i class='fas fa-clock fa-fw'></i>&nbsp;近期
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/categories/"
                
                  rel="nofollow"
                
                
                id="categories">
								<i class='fas fa-folder-open fa-fw'></i>&nbsp;分类
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/tags/"
                
                  rel="nofollow"
                
                
                id="tags">
								<i class='fas fa-hashtag fa-fw'></i>&nbsp;标签
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/archives/"
                
                  rel="nofollow"
                
                
                id="archives">
								<i class='fas fa-archive fa-fw'></i>&nbsp;归档
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/about/"
                
                  rel="nofollow"
                
                
                id="about">
								<i class='fas fa-info-circle fa-fw'></i>&nbsp;关于
							</a>
            </li>
          
       
      </ul>
		</nav>
    </header>
	</aside>
<script>setLoadingBarProgress(40);</script>



  <div class="l_body nocover">
    <div class='body-wrapper'>
      <div class='l_main'>
  

  <article id="post" class="post white-box article-type-post" itemscope itemprop="blogPost">
    


  <section class='meta'>
    
    
    <div class="meta" id="header-meta">
      
        
  
    <h1 class="title">
      <a href="/c/sed/">
        sed 命令从入门到精通
      </a>
    </h1>
  


      
      <div class='new-meta-box'>
        
          
        
          
            
  <div class='new-meta-item author'>
    
      <a href="/about" rel="nofollow">
        
          <i class="fas fa-user" aria-hidden="true"></i>
        
        <p>四叶草🍀</p>
      </a>
    
  </div>


          
        
          
            <div class="new-meta-item date">
  <a class='notlink'>
    <i class="fas fa-calendar-alt" aria-hidden="true"></i>
    <p>2019-11-29</p>
  </a>
</div>

          
        
          
            
  
  <div class='new-meta-item category'>
    <a href='/categories/%E6%95%99%E7%A8%8B/' rel="nofollow">
      <i class="fas fa-folder-open" aria-hidden="true"></i>
      <p>教程</p>
    </a>
  </div>


          
        
          
            

  <div class="new-meta-item markdown">
    
    
      <a href="https://github.com/fkxxyz/fkxxyz-blog-src/blob/master/source/_posts/c/sed.md" target="_blank" rel="nofollow noopener">
    
      <i class="fab fa-markdown"></i>
      <p>md源文档</p>
    </a>
  </div>



          
        
          
            

  <div class="new-meta-item download">
    
    
      <a href="https://github.com/fkxxyz/fkxxyz-blog-src/raw/master/source/_posts/c/sed.md" target="_blank" rel="nofollow noopener">
    
      <i class="fas fa-download"></i>
      <p>下载本文</p>
    </a>
  </div>



          
        
          
            
  
    <div class="new-meta-item browse busuanzi">
      <a class='notlink'>
        <i class="fas fa-eye" aria-hidden="true"></i>
        <p>
          <span id="busuanzi_value_page_pv">
            <i class="fas fa-spinner fa-spin fa-fw" aria-hidden="true"></i>
          </span>
        </p>
      </a>
    </div>
  


          
        
          
            

          
        
      </div>
      
        <hr>
      
    </div>
  </section>


    <section class="article typo">
      <div class="article-entry" itemprop="articleBody">
        <p>作为 Linux 三剑客之一的 sed，shell 编程中掌握它是很有必要的。</p>
<p>本文用实验的方式循序渐进地学习 sed，可谓最科学的学习方式，一点一点遍历 sed 所有知识，实现真正的精通，同时还能作为速查。</p>
<p>快来动手跟着一起做吧！</p>
<a id="more"></a>

<h2 id="入门实践"><a href="#入门实践" class="headerlink" title="入门实践"></a>入门实践</h2><p>边实践边学习是真正掌握技能的唯一途径，下面从易到难来实验 sed 的各个命令。</p>
<h3 id="实验准备"><a href="#实验准备" class="headerlink" title="实验准备"></a>实验准备</h3><p>为了更好的实验，在临时目录建立一个文本文件以便测试。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd /tmp</span><br><span class="line">mkdir a</span><br><span class="line">cd a</span><br></pre></td></tr></table></figure>

<p>创建一个文本文件 a.txt ，并写入十行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">for a in $(seq 1 1 10); do echo line$a; done &gt; a.txt</span><br><span class="line">cat a.txt</span><br></pre></td></tr></table></figure>

<p>以下是 a.txt 的内容</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h3 id="开始实验"><a href="#开始实验" class="headerlink" title="开始实验"></a>开始实验</h3><h4 id="a-命令-——-添加新行"><a href="#a-命令-——-添加新行" class="headerlink" title="a 命令 —— 添加新行"></a>a 命令 —— 添加新行</h4><p>在第 4 行的下面添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sed '4anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 4 表示第 4 行</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> a 表示添加</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> newline 表示要添加的行</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 为了方便代码阅读，也可以加个<span class="string">'\'</span>或空格，写成如下等价形式</span></span><br><span class="line">sed '4a\newline' a.txt</span><br><span class="line">sed '4a newline' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">newline</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<p>也可以一次性添加多行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed '4anewline1\nnewline2' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">newline1</span><br><span class="line">newline2</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="指定地址范围"><a href="#指定地址范围" class="headerlink" title="指定地址范围"></a>指定地址范围</h4><h5 id="——-范围分隔符"><a href="#——-范围分隔符" class="headerlink" title=", —— 范围分隔符"></a>, —— 范围分隔符</h5><p>从第 4 行到第 8 行的所有行的下面分别添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '4,8anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 4,8 表示从第4行到第8行，用逗号隔开地址</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">newline</span><br><span class="line">line5</span><br><span class="line">newline</span><br><span class="line">line6</span><br><span class="line">newline</span><br><span class="line">line7</span><br><span class="line">newline</span><br><span class="line">line8</span><br><span class="line">newline</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h5 id="——-到接下来几行"><a href="#——-到接下来几行" class="headerlink" title="+ —— 到接下来几行"></a>+ —— 到接下来几行</h5><p>从第 4 行到其后两行的每一行分别添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '4,+2anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 4,+2 表示从第4行到第4+2行，等同于 4,6</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">newline</span><br><span class="line">line5</span><br><span class="line">newline</span><br><span class="line">line6</span><br><span class="line">newline</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h5 id="——-倍数匹配"><a href="#——-倍数匹配" class="headerlink" title="~ —— 倍数匹配"></a>~ —— 倍数匹配</h5><p>从第 5 行到第一次遇到的 4 的倍数行的每一行分别添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '5,~4anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 5,~4 相当于第 5 行到从第 5 行开始，遇到行号为 4 的倍数的行为止，等价于 5,8</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">newline</span><br><span class="line">line6</span><br><span class="line">newline</span><br><span class="line">line7</span><br><span class="line">newline</span><br><span class="line">line8</span><br><span class="line">newline</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h5 id="——-步进"><a href="#——-步进" class="headerlink" title="~ —— 步进"></a>~ —— 步进</h5><p>从第 5 行起，所有行号为 2 的倍数的行后面添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '5~2anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 5~2 表示从第 5 行起，所有行号为 2 的倍数的行</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">newline</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">newline</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">newline</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h5 id="——-最后一行"><a href="#——-最后一行" class="headerlink" title="$ —— 最后一行"></a>$ —— 最后一行</h5><p>从第 4 行到最后一行的每一行分别添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sed '4,$anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> $ 表示最后一行</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 4,$ 表示从第 4 行到最后一行</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">newline</span><br><span class="line">line5</span><br><span class="line">newline</span><br><span class="line">line6</span><br><span class="line">newline</span><br><span class="line">line7</span><br><span class="line">newline</span><br><span class="line">line8</span><br><span class="line">newline</span><br><span class="line">line9</span><br><span class="line">newline</span><br><span class="line">line10</span><br><span class="line">newline</span><br></pre></td></tr></table></figure>



<h5 id="——-范围取反"><a href="#——-范围取反" class="headerlink" title="! —— 范围取反"></a>! —— 范围取反</h5><p>从第 4 行到第 8 行之外的所有行后面添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sed '4,8!anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> ! 表示地址取反</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 4,8! 表示从第 4 行到第 8 行之外的所有行</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">newline</span><br><span class="line">line2</span><br><span class="line">newline</span><br><span class="line">line3</span><br><span class="line">newline</span><br><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">newline</span><br><span class="line">line10</span><br><span class="line">newline</span><br></pre></td></tr></table></figure>



<h5 id="——-正则表达式范围"><a href="#——-正则表达式范围" class="headerlink" title="// —— 正则表达式范围"></a>// —— 正则表达式范围</h5><p>在匹配到 line4 的行后面添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">sed '/line4/anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> /line4/ 表示匹配到 line4 的行</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 等价于</span></span><br><span class="line">sed '\?line4?anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 其中 ? 可以替换成任意字符</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">newline</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<p>在匹配到 line4 到第 6 行的行后面添加一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '/line4/,6anewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> /line4/,6 表示匹配到 line4 到第 6 行的行</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">newline</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="i-命令-——-插入新行"><a href="#i-命令-——-插入新行" class="headerlink" title="i 命令 —— 插入新行"></a>i 命令 —— 插入新行</h4><p>在第 4 行前插入一行 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed '4inewline' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">newline</span><br><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="d-命令-——-删除"><a href="#d-命令-——-删除" class="headerlink" title="d 命令 —— 删除"></a>d 命令 —— 删除</h4><p>删除 4 到 8 行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '4,8d' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> d 表示删除</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="c-命令-——-行替换"><a href="#c-命令-——-行替换" class="headerlink" title="c 命令 —— 行替换"></a>c 命令 —— 行替换</h4><p>将 4 到 8 行替换成 newline</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '4,8cnewline' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> c 表示替换</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">newline</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="p-命令-——-打印"><a href="#p-命令-——-打印" class="headerlink" title="p 命令 —— 打印"></a>p 命令 —— 打印</h4><p>打印一遍 4 到 8 行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '4,8p' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> p 表示打印</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<p>只打印 4 到 8 行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed -n '4,8p' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> -n 表示不打印原文</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br></pre></td></tr></table></figure>



<p>从匹配到 “line4” 的行打印到匹配到 “ne6” 的行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed -n '/line4/,/e6/p' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 本例配合地址可灵活运用</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line6</span><br></pre></td></tr></table></figure>



<h4 id="命令-——-打印行号"><a href="#命令-——-打印行号" class="headerlink" title="= 命令 —— 打印行号"></a>= 命令 —— 打印行号</h4><p>从匹配到 “line4” 的行打印到匹配到 “ne6” 的行号</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed -n '/line4/,/e6/=' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> = 表示打印那一行的行号</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td></tr></table></figure>



<h4 id="s-命令-——-正则替换"><a href="#s-命令-——-正则替换" class="headerlink" title="s 命令 —— 正则替换"></a>s 命令 —— 正则替换</h4><p>将第 4 行到第 8 行的 ne 替换成 on</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sed '4,8s/ne/on/' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> s 表示替换</span></span><br><span class="line"><span class="meta">#</span><span class="bash">     格式为： s/正则表达式/要替换的内容/</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">lion4</span><br><span class="line">lion5</span><br><span class="line">lion6</span><br><span class="line">lion7</span><br><span class="line">lion8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<p>s 命令最后跟个 g 表示替换该行所有匹配</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo 'abcdabcd\nbcdebcde' | sed 's/b/x/g'</span><br></pre></td></tr></table></figure>

<p>输出结果为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">axcdaxcd</span><br><span class="line">xcdexcde</span><br></pre></td></tr></table></figure>

<p>而不加 g 的 s 只能替换每一行匹配到的第一个</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo 'abcdabcd\nbcdebcde' | sed 's/b/x/'</span><br></pre></td></tr></table></figure>

<p>输出结果为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">axcdabcd</span><br><span class="line">xcdebcde</span><br></pre></td></tr></table></figure>



<p>正则表达式的实例</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sed '4,8s/\(line\)\([[:digit:]]\+\)/\2 - \1/' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 4,8 表示从第 4 行到第 8 行</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> \(line\)\([[:digit:]]\+\) 是正则表达式</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> \1 表示正则表达式中第一个括号匹配的结果</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> \2 表示正则表达式中第二个括号匹配的结果</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 可以加 -r 参数开启扩展正则表达式，写成等价形式</span></span><br><span class="line">sed -r '4,8s/(line)([[:digit:]]+)/\2 - \1/' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">4 - line</span><br><span class="line">5 - line</span><br><span class="line">6 - line</span><br><span class="line">7 - line</span><br><span class="line">8 - line</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="y-命令-——-字符映射替换"><a href="#y-命令-——-字符映射替换" class="headerlink" title="y 命令 —— 字符映射替换"></a>y 命令 —— 字符映射替换</h4><p>将第 4 行到第 5 行按照字符映射 {e,i,l,n} -&gt; {o,c,e,h} 进行替换</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed '4,5y/eiln/oceh/' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">echo4</span><br><span class="line">echo5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="r-命令-——-行替换为文件内容"><a href="#r-命令-——-行替换为文件内容" class="headerlink" title="r 命令 —— 行替换为文件内容"></a>r 命令 —— 行替换为文件内容</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 先写入一个 b.txt 文件作为测试文件</span></span><br><span class="line">echo 'aaa\nbbb' &gt; b.txt</span><br><span class="line"></span><br><span class="line">sed '4,5rb.txt' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> r 表示以后面作为文件名读取文件内容进行替换</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">aaa</span><br><span class="line">bbb</span><br><span class="line">line5</span><br><span class="line">aaa</span><br><span class="line">bbb</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<h4 id="——-脚本块"><a href="#——-脚本块" class="headerlink" title="{} —— 脚本块"></a>{} —— 脚本块</h4><p>在大括号内用分号隔开命令，可以实现执行多个命令</p>
<p>打印 3 到 4 行和 7 到 8 行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sed -n '&#123;3,4p;7,8p&#125;' a.txt</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 最外层大括号可省略</span></span><br><span class="line">sed -n '3,4p;7,8p' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">line7</span><br><span class="line">line8</span><br></pre></td></tr></table></figure>



<p>如果每个命令的地址相同，那么也可以类似于像分配律一样的语法</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sed -n '4&#123;p;=&#125;' a.txt</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 等价于</span></span><br><span class="line">sed -n '&#123;4p;4=&#125;' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">line4</span><br><span class="line">4</span><br></pre></td></tr></table></figure>



<p>连 s 命令都能包含，体会到替换前和替换后的不同</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed -n '4&#123;p;s/ne/on/;p;=&#125;' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">line4</span><br><span class="line">lion4</span><br><span class="line">4</span><br></pre></td></tr></table></figure>



<p>大括号可以嵌套</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed -n '&#123;4&#123;p;=&#125;;8&#123;=;p&#125;&#125;' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">line4</span><br><span class="line">4</span><br><span class="line">8</span><br><span class="line">line8</span><br></pre></td></tr></table></figure>



<p>如果嵌套时，地址不同，则会取交集</p>
<ol>
<li><pre><code class="shell">sed -n '3,7{4,5p}' a.txt
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">输出结果</span><br></pre></td></tr></table></figure>
line4
line5
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">2. &#96;&#96;&#96;shell</span><br><span class="line">   sed -n &#39;4,7&#123;3,6p&#125;&#39; a.txt</span><br></pre></td></tr></table></figure>

输出结果

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line6</span><br></pre></td></tr></table></figure>


</code></pre>
</li>
</ol>
<h4 id="h-H-g-G-x-——-空间交换相关"><a href="#h-H-g-G-x-——-空间交换相关" class="headerlink" title="h,H,g,G,x —— 空间交换相关"></a>h,H,g,G,x —— 空间交换相关</h4><p>首先需要弄懂两个概念：模式空间、保持空间。</p>
<p><strong>模式空间：</strong>sed 命令默认是一行一行处理的，每读取到一行，会放到模式空间里，然后执行脚本来处理模式空间的内容，处理完后会清空模式空间，并且读取下一行继续处理，如此循环直到行尾。模式空间相当于专门用于 sed 处理的字符串缓冲区。</p>
<p><strong>保持空间：</strong>默认的 sed 是不会操作保持空间的，保持空间是专门为用户提供的空间，这几个命令可以操作保持空间，来实现更复杂的功能。</p>
<blockquote>
<p><strong>h：</strong>把模式空间内容覆盖到保持空间中<br><strong>H：</strong>把模式空间内容追加到保持空间中<br><strong>g：</strong>把保持空间内容覆盖到模式空间中<br><strong>G：</strong>把保持空间内容追加到模式空间中<br><strong>x：</strong>交换模式空间与保持空间的内容</p>
</blockquote>
<p>将第 4 行和第 6 行复制到第 8 行后面。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sed '&#123;4h;6H;8G&#125;' a.txt</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 4h 表示将第 4 行放入保持空间</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 6H 表示将第 6 行加入保持空间</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 8G 表示将保持空间内容追加到模式空间</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">line5</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line4</span><br><span class="line">line6</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>

<h4 id="n-N-P-D-——-模式空间操作"><a href="#n-N-P-D-——-模式空间操作" class="headerlink" title="n,N,P,D —— 模式空间操作"></a>n,N,P,D —— 模式空间操作</h4><p>有了模式空间的概念后，可以重新理解一下前面 p、d、n 等命令的本质。</p>
<blockquote>
<p><strong>p：</strong>打印当前模式空间所有内容，追加到默认输出之后。</p>
<p><strong>P：</strong>打印当前模式空间开端至\n的内容，并追加到默认输出之前。Sed并不对每行末尾\n进行处理，但是对N命令追加的行间\n进行处理，因为此时sed将两行看做一行。</p>
<p><strong>n：</strong>命令简单来说就是提前读取下一行，覆盖模型空间前一行，然后执行后续命令。然后再读取新行，对新读取的内容重头执行sed。</p>
<p><strong>N：</strong>命令简单来说就是追加下一行到模式空间，同时将两行看做一行，但是两行之间依然含有\n换行符，然后执行后续命令。然后再读取新行，对新读取的内容重头执行sed。此时，新读取的行会覆盖之前的行（之前的两行已经合并为一行）。</p>
<p><strong>d：</strong>命令是删除当前模式空间内容（不再传至标准输出）， 并放弃之后的命令，并对新读取的内容，重头执行sed。</p>
<p><strong>D：</strong>命令是删除当前模式空间开端至\n的内容（不在传至标准输出）， 放弃之后的命令，但是对剩余模式空间重新执行sed。</p>
</blockquote>
<p>删除匹配 line4 行的下一行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '/line4/&#123;n;d&#125;' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 操作到第 4 行时，提前读取第 5 行并删掉。</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line4</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>





<p>打印偶数行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">sed -n '&#123;n;p&#125;' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> n 命令表示操作每一行时，读取下一行，覆盖掉已经读取的这一行</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> p 表示打印出来</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 每次循环时，模式空间中，奇数行全部被偶数行覆盖</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 打印偶数行也可以用步进地址实现</span></span><br><span class="line">sed -n '2~2p' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">line2</span><br><span class="line">line4</span><br><span class="line">line6</span><br><span class="line">line8</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<p>删除匹配到 line4 行和下一行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sed '/line4/&#123;N;d&#125;' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 操作到第 4 行时，提前读取第 5 行追加到模式空间，然后两行一起删掉。</span></span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line2</span><br><span class="line">line3</span><br><span class="line">line6</span><br><span class="line">line7</span><br><span class="line">line8</span><br><span class="line">line9</span><br><span class="line">line10</span><br></pre></td></tr></table></figure>



<p>打印奇数行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sed –n '$!N;P' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 除了最后一行之外所有行，都提前读取下一行追加到模式空间，然后打印追加前的第一行，最后一行单独输出</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 打印奇数行也可以用步进地址实现</span></span><br><span class="line">sed -n '1~2p' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line3</span><br><span class="line">line5</span><br><span class="line">line7</span><br><span class="line">line9</span><br></pre></td></tr></table></figure>



<p>每次提前读取三行，输出第一行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed -n '&#123;N;N;P&#125;' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">line1</span><br><span class="line">line4</span><br><span class="line">line7</span><br></pre></td></tr></table></figure>



<h4 id="b-——-标签跳转"><a href="#b-——-标签跳转" class="headerlink" title="b —— 标签跳转"></a>b —— 标签跳转</h4><p><strong>b：</strong>跳转到用冒号开头表示的标签</p>
<p>用标签跳转的方式实现：将除了匹配 line4 的行其它行的 ne 换成 on</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">sed '/line4/bend;s/ne/on/;:end' a.txt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 匹配到 line4 时，会执行 b 命令，跳转到最后的 :end 标签，也就相当于跳过了替换命令</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 如果标签在前可能造成死循环，需要注意指定行</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 当标签在最后时，可以省略，而且当 b 命令指定的标签不存在时，会自动跳转到最后</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 所以可简写成</span></span><br><span class="line">sed '/line4/b;s/ne/on/' a.txt</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 当然，可以用前面的地址范围取反来实现</span></span><br><span class="line">sed '/line4/!s/ne/on/' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">lion1</span><br><span class="line">lion2</span><br><span class="line">lion3</span><br><span class="line">line4</span><br><span class="line">lion5</span><br><span class="line">lion6</span><br><span class="line">lion7</span><br><span class="line">lion8</span><br><span class="line">lion9</span><br><span class="line">lion10</span><br></pre></td></tr></table></figure>



<h4 id="t-——-条件标签跳转"><a href="#t-——-条件标签跳转" class="headerlink" title="t —— 条件标签跳转"></a>t —— 条件标签跳转</h4><p><strong>t：</strong>如果前一条命令执行成功，则跳转到用冒号开头表示的标签。</p>
<p>用条件标签跳转的方式实现：将除了匹配 line4 的行其它行的 ne 换成 on，4换成 F</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sed 's/4/F/;tend;s/ne/on/;:end' a.txt</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 当标签在最后时，可以省略，而且当 b 命令指定的标签不存在时，会自动跳转到最后</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 所以可简写成</span></span><br><span class="line">sed 's/4/F/;t;s/ne/on/' a.txt</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 当然，可以用前面的地址范围取反来实现</span></span><br><span class="line">sed '/4/!s/ne/on/;/4/s/4/F/' a.txt</span><br></pre></td></tr></table></figure>

<p>输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">lion1</span><br><span class="line">lion2</span><br><span class="line">lion3</span><br><span class="line">lineF</span><br><span class="line">lion5</span><br><span class="line">lion6</span><br><span class="line">lion7</span><br><span class="line">lion8</span><br><span class="line">lion9</span><br><span class="line">lion10</span><br></pre></td></tr></table></figure>



<h2 id="速查"><a href="#速查" class="headerlink" title="速查"></a>速查</h2><p><em>以下内容使用谷歌翻译自 <a href="https://linux.die.net/man/1/sed" target="_blank" rel="noopener">sed(1) - Linux man page</a></em> 以及 sed –help，可供参考。</p>
<h3 id="名称"><a href="#名称" class="headerlink" title="名称"></a>名称</h3><p>sed - 流编辑器，用于过滤和转换文本</p>
<h3 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h3><p>sed [可选参数]… {脚本} [输入文件]…</p>
<p>如果未提供-e，-expression，-f或–file选项，则将第一个非可选参数用作要解释的sed脚本。 其余所有参数均为输入文件的名称； 如果未指定输入文件，那么将读取标准输入。</p>
<h3 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h3><p>Sed是流编辑器。 流编辑器用于对输入流（文件或来自管道的输入）执行基本的文本转换。 尽管在某种程度上类似于允许脚本编辑（例如ed）的编辑器，但sed通过仅对输入进行一次传递来工作，因此效率更高。 但是sed能够过滤管道中的文本，这使其与其他类型的编辑器特别有区别。</p>
<h4 id="n-–quiet-–silent"><a href="#n-–quiet-–silent" class="headerlink" title="-n, –quiet, –silent"></a>-n, –quiet, –silent</h4><p>禁止自动打印模式空间</p>
<h4 id="e-脚本-–expression-脚本"><a href="#e-脚本-–expression-脚本" class="headerlink" title="-e 脚本, –expression=脚本"></a>-e 脚本, –expression=脚本</h4><p>添加“脚本”到程序的运行列表</p>
<h4 id="f-脚本文件-–file-脚本文件"><a href="#f-脚本文件-–file-脚本文件" class="headerlink" title="-f 脚本文件, –file=脚本文件"></a>-f 脚本文件, –file=脚本文件</h4><p>添加“脚本文件”到程序的运行列表</p>
<h4 id="–follow-symlinks"><a href="#–follow-symlinks" class="headerlink" title="–follow-symlinks"></a>–follow-symlinks</h4><p>直接修改文件时跟随符号链接； 硬链接仍然会断开。</p>
<h4 id="i-扩展名-–in-place-扩展名"><a href="#i-扩展名-–in-place-扩展名" class="headerlink" title="-i[扩展名], –in-place[=扩展名]"></a>-i[扩展名], –in-place[=扩展名]</h4><p>直接修改文件（如果指定扩展名则备份文件）</p>
<h4 id="c-–copy"><a href="#c-–copy" class="headerlink" title="-c, –copy"></a>-c, –copy</h4><p>在 -i 模式下直接修改文件且备份时，使用复制操作而不是重命名。 尽管这样做可以避免断开链接（符号链接或硬链接），但最终的编辑操作不是原子的。 这很少是理想的模式。 –follow-symlinks通常就足够了，并且更快，更安全。</p>
<h4 id="l-N-–line-length-N"><a href="#l-N-–line-length-N" class="headerlink" title="-l N, –line-length=N"></a>-l N, –line-length=N</h4><p>指定“l”命令的换行期望长度</p>
<h4 id="–posix"><a href="#–posix" class="headerlink" title="–posix"></a>–posix</h4><p>关闭所有 GNU 扩展</p>
<h4 id="E-r-–regexp-extended"><a href="#E-r-–regexp-extended" class="headerlink" title="-E, -r, –regexp-extended"></a>-E, -r, –regexp-extended</h4><p>在脚本中使用扩展正则表达式（为保证可移植性使用 POSIX -E）。</p>
<h4 id="s-–separate"><a href="#s-–separate" class="headerlink" title="-s, –separate"></a>-s, –separate</h4><p>将输入文件视为各个独立的文件而不是单个长的连续输入流</p>
<h4 id="u-–unbuffered"><a href="#u-–unbuffered" class="headerlink" title="-u, –unbuffered"></a>-u, –unbuffered</h4><p>从输入文件读取最少的数据，更频繁的刷新输出</p>
<h4 id="–help"><a href="#–help" class="headerlink" title="–help"></a>–help</h4><p>打印帮助并退出</p>
<h4 id="–version"><a href="#–version" class="headerlink" title="–version"></a>–version</h4><p>输出版本信息并退出</p>
<h3 id="命令语法"><a href="#命令语法" class="headerlink" title="命令语法"></a>命令语法</h3><p>这只是sed命令的简要提要，以提示那些已经知道sed的人。 必须参考其他文档（例如texinfo文档）以获取更完整的描述。</p>
<h4 id="标签"><a href="#标签" class="headerlink" title=": 标签"></a>: 标签</h4><p><strong>b</strong> 和 <strong>t</strong> 命令的标签</p>
<h4 id="注释"><a href="#注释" class="headerlink" title="# 注释"></a># 注释</h4><p>注释会一直延伸到下一个换行符（或*<em>-e *</em>脚本片段的末尾）。</p>
<h4 id=""><a href="#" class="headerlink" title="}"></a>}</h4><p>关闭 {}块 的括号的右半括号</p>
<h4 id="-1"><a href="#-1" class="headerlink" title="="></a>=</h4><p>打印当前行号</p>
<h4 id="a-文本"><a href="#a-文本" class="headerlink" title="a 文本"></a>a 文本</h4><p>在模式空间后追加文本，该文本的每个嵌入换行符前都有一个反斜杠。</p>
<h4 id="i-文本"><a href="#i-文本" class="headerlink" title="i 文本"></a>i 文本</h4><p>在模式空间前插入文本，该文本的每个嵌入换行符前都有一个反斜杠。</p>
<h4 id="q-退出代码"><a href="#q-退出代码" class="headerlink" title="q [退出代码]"></a>q [退出代码]</h4><p>立即退出 sed 脚本而不处理任何其他输入，除非如果未禁用自动打印，将打印当前图案空间。 退出代码参数是GNU扩展。</p>
<h4 id="Q-退出代码"><a href="#Q-退出代码" class="headerlink" title="Q [退出代码]"></a>Q [退出代码]</h4><p>立即退出 sed 脚本，而不处理更多输入。 这是一个GNU扩展。</p>
<h4 id="r-文件名"><a href="#r-文件名" class="headerlink" title="r 文件名"></a>r 文件名</h4><p>从文件中读取的文本追加到模式空间中。</p>
<h4 id="R-文件名"><a href="#R-文件名" class="headerlink" title="R 文件名"></a>R 文件名</h4><p>从文件中读取的文本追加到模式空间中。 每次调用该命令都会从文件中读取一行。这是一个GNU扩展。</p>
<h4 id="-2"><a href="#-2" class="headerlink" title="{"></a>{</h4><p>启动 {}块 的括号的左半括号</p>
<h4 id="b-标签"><a href="#b-标签" class="headerlink" title="b [标签]"></a>b [标签]</h4><p>跳转到标签； 如果省略了标签参数，则跳转到脚本结尾。</p>
<h4 id="t-标签"><a href="#t-标签" class="headerlink" title="t [标签]"></a>t [标签]</h4><p>如果上一条读取的输入行被 s/// 命令替换成功，则跳转至标签 ; 如果省略标签，则跳转到脚本结尾。</p>
<h4 id="T-标签"><a href="#T-标签" class="headerlink" title="T [标签]"></a>T [标签]</h4><p>如果上一条读取的输入行没有被 s/// 命令替换成功，则跳转至标签 ; 如果省略标签，则跳转到脚本结尾。这是一个GNU扩展。</p>
<h4 id="c-文本"><a href="#c-文本" class="headerlink" title="c 文本"></a>c 文本</h4><p>将所选行替换为文本，该文本的每个嵌入换行符前都有一个反斜杠。</p>
<h4 id="d"><a href="#d" class="headerlink" title="d"></a>d</h4><p>删除模式空间。 开始下一个循环。</p>
<h4 id="D"><a href="#D" class="headerlink" title="D"></a>D</h4><p>删除模式空间中的第一个嵌入的行。 从下一个循环开始，但是如果模式空间中仍有数据，则跳过从输入中读取的操作。</p>
<h4 id="h-H"><a href="#h-H" class="headerlink" title="h H"></a>h H</h4><p>复制/追加模式空间到保持空间。</p>
<h4 id="g-G"><a href="#g-G" class="headerlink" title="g G"></a>g G</h4><p>复制/追加保持空间到模式空间。</p>
<h4 id="x"><a href="#x" class="headerlink" title="x"></a>x</h4><p>交换保持空间和模式空间的内容。</p>
<h4 id="l"><a href="#l" class="headerlink" title="l"></a>l</h4><p>以“视觉清晰”的形式列出当前行。</p>
<h4 id="l-宽度"><a href="#l-宽度" class="headerlink" title="l 宽度"></a>l 宽度</h4><p>以“视觉清晰”的形式列出当前行，并以根据指定宽度的字符将其断开。 这是一个GNU扩展。</p>
<h4 id="n-N"><a href="#n-N" class="headerlink" title="n N"></a>n N</h4><p>将输入的下一行读取/追加到模式空间。</p>
<h4 id="p"><a href="#p" class="headerlink" title="p"></a>p</h4><p>打印当前模式空间</p>
<h4 id="P"><a href="#P" class="headerlink" title="P"></a>P</h4><p>打印当前模式空间直到第一个嵌入式换行符。</p>
<h4 id="s-正则表达式-替代串"><a href="#s-正则表达式-替代串" class="headerlink" title="s/正则表达式/替代串/"></a>s/正则表达式/替代串/</h4><p>尝试将正则表达式与模式空间进行匹配。 如果成功，则用替代串/替换该部分。替代串可以包含特殊字符 “&amp;” 来表示匹配的模式空间部分，而特殊转义 \1 到 \9 则表示正则表达式中的相应匹配子表达式。</p>
<h4 id="w-文件名"><a href="#w-文件名" class="headerlink" title="w 文件名"></a>w 文件名</h4><p>将当前模式空间写入文件。</p>
<h4 id="W-文件名"><a href="#W-文件名" class="headerlink" title="W 文件名"></a>W 文件名</h4><p>将当前模式空间的第一行写入文件。 这是一个GNU扩展。</p>
<h4 id="y-源串-目标串"><a href="#y-源串-目标串" class="headerlink" title="y/源串/目标串/"></a>y/源串/目标串/</h4><p>将模式空间中出现在源串中的字符翻译成目标串中的相应字符。</p>
<h3 id="地址"><a href="#地址" class="headerlink" title="地址"></a>地址</h3><p>sed命令可以不带地址，在这种情况下，将对所有输入行执行该命令。 具有一个地址，在这种情况下，该命令仅对与该地址匹配的输入行执行； 或使用两个地址，在这种情况下，将对所有输入行执行命令，这些输入行与从第一个地址开始一直延伸到第二个地址的所有行包括在内。 有关地址范围的三点注意事项：语法为“地址1,地址2”（即地址用逗号分隔）； 即使地址2选择了较早的行，也将始终接受与地址匹配的行； 如果地址2是正则表达式，则不会针对地址1匹配的行进行测试。</p>
<p>在地址（或地址范围）之后，在命令之前，! 可以插入，它指定仅当地址（或地址范围）不匹配时才执行命令。</p>
<h4 id="数字"><a href="#数字" class="headerlink" title="数字"></a>数字</h4><p>仅匹配指定的数字行。</p>
<h4 id="首行-步进"><a href="#首行-步进" class="headerlink" title="首行~步进"></a>首行~步进</h4><p>匹配从首行开始的每个相隔步进的行。 例如，“ sed -n 1<del>2p”将打印输入流中的所有奇数行，并且地址2</del>5将与第二行开始的每第五行匹配。首行可以为零； 在这种情况下，sed 的操作就好像等于步进。 （这是一个扩展。）</p>
<h4 id="-3"><a href="#-3" class="headerlink" title="$"></a>$</h4><p>表示最后一行。</p>
<h4 id="正则表达式"><a href="#正则表达式" class="headerlink" title="/正则表达式/"></a>/正则表达式/</h4><p>匹配正则表达式的行。</p>
<h4 id="c正则表达式c"><a href="#c正则表达式c" class="headerlink" title="\c正则表达式c"></a>\c正则表达式c</h4><p>匹配正则表达式的行。c 可以是任何字符。</p>
<h3 id="GNU-sed-还支持一些特殊的2地址形式"><a href="#GNU-sed-还支持一些特殊的2地址形式" class="headerlink" title="GNU sed 还支持一些特殊的2地址形式"></a>GNU sed 还支持一些特殊的2地址形式</h3><h4 id="0-地址2"><a href="#0-地址2" class="headerlink" title="0,地址2"></a>0,地址2</h4><p>从“匹配的第一个地址”状态开始，直到找到地址2。 这类似于“1,地址2”，不同之处在于，如果地址2与输入的第一行匹配，则“0,地址2”形式将在其范围的末尾，而“1,地址2”形式仍为在其范围的开始。仅当地址2为正则表达式时有效。</p>
<h4 id="地址1-N"><a href="#地址1-N" class="headerlink" title="地址1,+N"></a>地址1,+N</h4><p>将匹配地址1和地址1之后的 N 行。</p>
<h4 id="地址1-N-1"><a href="#地址1-N-1" class="headerlink" title="地址1,~N"></a>地址1,~N</h4><p>将匹配地址1和地址1之后的行，直到输入行号是 N 的倍数的下一行。</p>
<h3 id="正则表达式-1"><a href="#正则表达式-1" class="headerlink" title="正则表达式"></a>正则表达式</h3><p>本来应该支持POSIX.2 BRE，但是由于性能问题，它们并不完全支持。 正则表达式中的 \n 序列与换行符匹配，对于 \a，\t 和其他序列也是如此。</p>
<h2 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h2><ol>
<li><a href="https://linux.die.net/man/1/sed" target="_blank" rel="noopener">sed(1) - Linux man page</a></li>
<li><a href="https://www.runoob.com/linux/linux-comm-sed.html" target="_blank" rel="noopener">菜鸟教程 —— Linux sed 命令</a></li>
<li><a href="https://www.cnblogs.com/liwei0526vip/p/5644163.html" target="_blank" rel="noopener">肖邦linux —— sed入门详解教程</a></li>
<li><a href="https://www.cnblogs.com/irockcode/p/8018575.html" target="_blank" rel="noopener">DataCareer —— Sed命令n，N，d，D，p，P，h，H，g，G，x解析</a></li>
</ol>

      </div>
      
      
        <br>
        


  <section class='meta' id="footer-meta">
    <div class='new-meta-box'>
      
        
          <div class="new-meta-item date" itemprop="dateUpdated" datetime="2019-12-18T18:10:14+08:00">
  <a class='notlink'>
    <i class="fas fa-clock" aria-hidden="true"></i>
    <p>更新于 2019年12月18日</p>
  </a>
</div>

        
      
        
          
  
  <div class="new-meta-item meta-tags"><a class="tag" href="/tags/linux/" rel="nofollow"><i class="fas fa-tag" aria-hidden="true"></i><p>linux</p></a></div> <div class="new-meta-item meta-tags"><a class="tag" href="/tags/shell/" rel="nofollow"><i class="fas fa-tag" aria-hidden="true"></i><p>shell</p></a></div> <div class="new-meta-item meta-tags"><a class="tag" href="/tags/sed/" rel="nofollow"><i class="fas fa-tag" aria-hidden="true"></i><p>sed</p></a></div>


        
      
        
          
  <div class="new-meta-item share -mob-share-list">
  <div class="-mob-share-list share-body">
    
      
        <a class="-mob-share-qq" title="QQ好友" rel="external nofollow noopener noreferrer"
          
          href="http://connect.qq.com/widget/shareqq/index.html?url=https://www.fkxxyz.com/c/sed/&title=sed 命令从入门到精通 | 四叶草&summary=作为 Linux 三剑客之一的 sed，shell 编程中掌握它是很有必要的。
本文用实验的方式循序渐进地学习 sed，可谓最科学的学习方式，一点一点遍历 sed 所有知识，实现真正的精通，同时还能作为速查。
快来动手跟着一起做吧！"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/qq.png">
          
        </a>
      
    
      
        <a class="-mob-share-qzone" title="QQ空间" rel="external nofollow noopener noreferrer"
          
          href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://www.fkxxyz.com/c/sed/&title=sed 命令从入门到精通 | 四叶草&summary=作为 Linux 三剑客之一的 sed，shell 编程中掌握它是很有必要的。
本文用实验的方式循序渐进地学习 sed，可谓最科学的学习方式，一点一点遍历 sed 所有知识，实现真正的精通，同时还能作为速查。
快来动手跟着一起做吧！"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/qzone.png">
          
        </a>
      
    
      
        <a class="-mob-share-weibo" title="微博" rel="external nofollow noopener noreferrer"
          
          href="http://service.weibo.com/share/share.php?url=https://www.fkxxyz.com/c/sed/&title=sed 命令从入门到精通 | 四叶草&summary=作为 Linux 三剑客之一的 sed，shell 编程中掌握它是很有必要的。
本文用实验的方式循序渐进地学习 sed，可谓最科学的学习方式，一点一点遍历 sed 所有知识，实现真正的精通，同时还能作为速查。
快来动手跟着一起做吧！"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/weibo.png">
          
        </a>
      
    
  </div>
</div>



        
      
    </div>
  </section>


      
      
          <div class="prev-next">
              
                  <section class="prev">
                      <span class="art-item-left">
                          <h6><i class="fas fa-chevron-left" aria-hidden="true"></i>&nbsp;上一页</h6>
                          <h4>
                              <a href="/learn/qnote/" rel="prev" title="关于手机和电脑之间文字快速互通">
                                
                                    关于手机和电脑之间文字快速互通
                                
                              </a>
                          </h4>
                          
                              
                              <h6 class="tags">
                                  <a class="tag" href="/tags/%E5%89%8D%E7%AB%AF/"><i class="fas fa-tag fa-fw" aria-hidden="true"></i> 前端</a> <a class="tag" href="/tags/php/"><i class="fas fa-tag fa-fw" aria-hidden="true"></i> php</a>
                              </h6>
                          
                      </span>
                  </section>
              
              
                  <section class="next">
                      <span class="art-item-right" aria-hidden="true">
                          <h6>下一页&nbsp;<i class="fas fa-chevron-right" aria-hidden="true"></i></h6>
                          <h4>
                              <a href="/learn/excel-crack/" rel="prev" title="记一次 excel 文档的密码破解的探索">
                                  
                                      记一次 excel 文档的密码破解的探索
                                  
                              </a>
                          </h4>
                          
                              
                              <h6 class="tags">
                                  <a class="tag" href="/tags/python/"><i class="fas fa-tag fa-fw" aria-hidden="true"></i> python</a> <a class="tag" href="/tags/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8/"><i class="fas fa-tag fa-fw" aria-hidden="true"></i> 信息安全</a>
                              </h6>
                          
                      </span>
                  </section>
              
          </div>
      
    </section>
  </article>



  <!-- 显示推荐文章和评论 -->



  






<!-- 根据页面mathjax变量决定是否加载MathJax数学公式js -->



  <script>
    window.subData = {
      title: 'sed 命令从入门到精通',
      tools: true
    }
  </script>


</div>
<aside class='l_side'>
  
    
    
      
      
        
          
          
        
          
          
        
          
          
        
          
          
            
              <section class='widget grid'>
  
<header class='material'>
  <div><i class="fas fa-map-signs fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;站内导航</div>
  
</header>

  <div class='content material'>
    <ul class="grid navgation">
      
        <li><a class="flat-box" title="/" href="/"
          
          
          id="home">
          
            <i class="fas fa-clock fa-fw" aria-hidden="true"></i>
          
          近期文章
        </a></li>
      
        <li><a class="flat-box" title="/archives/" href="/archives/"
          
            rel="nofollow"
          
          
          id="archives">
          
            <i class="fas fa-archive fa-fw" aria-hidden="true"></i>
          
          文章归档
        </a></li>
      
        <li><a class="flat-box" title="/about/" href="/about/"
          
            rel="nofollow"
          
          
          id="about">
          
            <i class="fas fa-info-circle fa-fw" aria-hidden="true"></i>
          
          关于小站
        </a></li>
      
    </ul>
  </div>
</section>

            
          
        
          
          
        
          
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
            
              
  <section class='widget category'>
    
<header class='material'>
  <div><i class="fas fa-folder-open fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;文章分类</div>
  
    <a class="rightBtn"
    
      rel="nofollow"
    
    
    href="/categories/"
    title="categories/">
    <i class="fas fa-expand-arrows-alt fa-fw"></i></a>
  
</header>

    <div class='content material'>
      <ul class="entry">
        
          <li><a class="flat-box" title="/categories/%E5%8E%9F%E5%88%9B%E5%BC%80%E5%8F%91/" href="/categories/%E5%8E%9F%E5%88%9B%E5%BC%80%E5%8F%91/"><div class='name'>原创开发</div><div class='badge'>(5)</div></a></li>
        
          <li><a class="flat-box" title="/categories/%E6%8E%A2%E7%A9%B6%E5%AD%A6%E4%B9%A0/" href="/categories/%E6%8E%A2%E7%A9%B6%E5%AD%A6%E4%B9%A0/"><div class='name'>探究学习</div><div class='badge'>(6)</div></a></li>
        
          <li><a class="flat-box" title="/categories/%E6%95%99%E7%A8%8B/" href="/categories/%E6%95%99%E7%A8%8B/"><div class='name'>教程</div><div class='badge'>(7)</div></a></li>
        
          <li><a class="flat-box" title="/categories/%E8%AE%B0%E5%BD%95/" href="/categories/%E8%AE%B0%E5%BD%95/"><div class='name'>记录</div><div class='badge'>(1)</div></a></li>
        
      </ul>
    </div>
  </section>


            
          
        
          
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
            
              
  <section class='widget tagcloud'>
    
<header class='material'>
  <div><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;热门标签</div>
  
    <a class="rightBtn"
    
      rel="nofollow"
    
    
    href="/tags/"
    title="tags/">
    <i class="fas fa-expand-arrows-alt fa-fw"></i></a>
  
</header>

    <div class='content material'>
      <a href="/tags/archlinux/" style="font-size: 24px; color: #555">archlinux</a> <a href="/tags/fcitx/" style="font-size: 16.5px; color: #888">fcitx</a> <a href="/tags/hexo/" style="font-size: 16.5px; color: #888">hexo</a> <a href="/tags/linux/" style="font-size: 19px; color: #777">linux</a> <a href="/tags/php/" style="font-size: 14px; color: #999">php</a> <a href="/tags/python/" style="font-size: 21.5px; color: #666">python</a> <a href="/tags/rime/" style="font-size: 14px; color: #999">rime</a> <a href="/tags/sed/" style="font-size: 14px; color: #999">sed</a> <a href="/tags/shell/" style="font-size: 14px; color: #999">shell</a> <a href="/tags/wine/" style="font-size: 16.5px; color: #888">wine</a> <a href="/tags/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8/" style="font-size: 16.5px; color: #888">信息安全</a> <a href="/tags/%E5%89%8D%E7%AB%AF/" style="font-size: 14px; color: #999">前端</a> <a href="/tags/%E5%8D%B3%E5%85%B4%E4%BC%B4%E5%A5%8F/" style="font-size: 14px; color: #999">即兴伴奏</a> <a href="/tags/%E5%8F%8C%E6%98%BE%E5%8D%A1%E5%88%87%E6%8D%A2/" style="font-size: 14px; color: #999">双显卡切换</a> <a href="/tags/%E5%92%8C%E5%BC%A6/" style="font-size: 14px; color: #999">和弦</a> <a href="/tags/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/" style="font-size: 16.5px; color: #888">思维导图</a> <a href="/tags/%E6%A1%8C%E9%9D%A2%E7%8E%AF%E5%A2%83/" style="font-size: 14px; color: #999">桌面环境</a> <a href="/tags/%E7%9A%AE%E8%82%A4/" style="font-size: 14px; color: #999">皮肤</a> <a href="/tags/%E8%BE%93%E5%85%A5%E6%B3%95/" style="font-size: 16.5px; color: #888">输入法</a> <a href="/tags/%E9%9F%B3%E4%B9%90/" style="font-size: 14px; color: #999">音乐</a>
    </div>
  </section>


            
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
            
              
  <section class='widget toc-wrapper'>
    
<header class='material'>
  <div><i class="fas fa-list fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;本文目录</div>
  
    <!-- <div class='wrapper'><a class="s-toc rightBtn" rel="external nofollow noopener noreferrer" href="javascript:void(0)"><i class="fas fa-thumbtack fa-fw"></i></a></div> -->
  
</header>

    <div class='content material'>
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#入门实践"><span class="toc-text">入门实践</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#实验准备"><span class="toc-text">实验准备</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#开始实验"><span class="toc-text">开始实验</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#a-命令-——-添加新行"><span class="toc-text">a 命令 —— 添加新行</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#指定地址范围"><span class="toc-text">指定地址范围</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#——-范围分隔符"><span class="toc-text">, —— 范围分隔符</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#——-到接下来几行"><span class="toc-text">+ —— 到接下来几行</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#——-倍数匹配"><span class="toc-text">~ —— 倍数匹配</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#——-步进"><span class="toc-text">~ —— 步进</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#——-最后一行"><span class="toc-text">$ —— 最后一行</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#——-范围取反"><span class="toc-text">! —— 范围取反</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#——-正则表达式范围"><span class="toc-text">&#x2F;&#x2F; —— 正则表达式范围</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#i-命令-——-插入新行"><span class="toc-text">i 命令 —— 插入新行</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#d-命令-——-删除"><span class="toc-text">d 命令 —— 删除</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#c-命令-——-行替换"><span class="toc-text">c 命令 —— 行替换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#p-命令-——-打印"><span class="toc-text">p 命令 —— 打印</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#命令-——-打印行号"><span class="toc-text">&#x3D; 命令 —— 打印行号</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#s-命令-——-正则替换"><span class="toc-text">s 命令 —— 正则替换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#y-命令-——-字符映射替换"><span class="toc-text">y 命令 —— 字符映射替换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#r-命令-——-行替换为文件内容"><span class="toc-text">r 命令 —— 行替换为文件内容</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#——-脚本块"><span class="toc-text">{} —— 脚本块</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#h-H-g-G-x-——-空间交换相关"><span class="toc-text">h,H,g,G,x —— 空间交换相关</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#n-N-P-D-——-模式空间操作"><span class="toc-text">n,N,P,D —— 模式空间操作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#b-——-标签跳转"><span class="toc-text">b —— 标签跳转</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#t-——-条件标签跳转"><span class="toc-text">t —— 条件标签跳转</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#速查"><span class="toc-text">速查</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#名称"><span class="toc-text">名称</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#语法"><span class="toc-text">语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#描述"><span class="toc-text">描述</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#n-–quiet-–silent"><span class="toc-text">-n, –quiet, –silent</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#e-脚本-–expression-脚本"><span class="toc-text">-e 脚本, –expression&#x3D;脚本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#f-脚本文件-–file-脚本文件"><span class="toc-text">-f 脚本文件, –file&#x3D;脚本文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#–follow-symlinks"><span class="toc-text">–follow-symlinks</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#i-扩展名-–in-place-扩展名"><span class="toc-text">-i[扩展名], –in-place[&#x3D;扩展名]</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#c-–copy"><span class="toc-text">-c, –copy</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#l-N-–line-length-N"><span class="toc-text">-l N, –line-length&#x3D;N</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#–posix"><span class="toc-text">–posix</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#E-r-–regexp-extended"><span class="toc-text">-E, -r, –regexp-extended</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#s-–separate"><span class="toc-text">-s, –separate</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#u-–unbuffered"><span class="toc-text">-u, –unbuffered</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#–help"><span class="toc-text">–help</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#–version"><span class="toc-text">–version</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#命令语法"><span class="toc-text">命令语法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#标签"><span class="toc-text">: 标签</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#注释"><span class="toc-text"># 注释</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#"><span class="toc-text">}</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#-1"><span class="toc-text">&#x3D;</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#a-文本"><span class="toc-text">a 文本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#i-文本"><span class="toc-text">i 文本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#q-退出代码"><span class="toc-text">q [退出代码]</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Q-退出代码"><span class="toc-text">Q [退出代码]</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#r-文件名"><span class="toc-text">r 文件名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#R-文件名"><span class="toc-text">R 文件名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#-2"><span class="toc-text">{</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#b-标签"><span class="toc-text">b [标签]</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#t-标签"><span class="toc-text">t [标签]</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#T-标签"><span class="toc-text">T [标签]</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#c-文本"><span class="toc-text">c 文本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#d"><span class="toc-text">d</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#D"><span class="toc-text">D</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#h-H"><span class="toc-text">h H</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#g-G"><span class="toc-text">g G</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#x"><span class="toc-text">x</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#l"><span class="toc-text">l</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#l-宽度"><span class="toc-text">l 宽度</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#n-N"><span class="toc-text">n N</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#p"><span class="toc-text">p</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#P"><span class="toc-text">P</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#s-正则表达式-替代串"><span class="toc-text">s&#x2F;正则表达式&#x2F;替代串&#x2F;</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#w-文件名"><span class="toc-text">w 文件名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#W-文件名"><span class="toc-text">W 文件名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#y-源串-目标串"><span class="toc-text">y&#x2F;源串&#x2F;目标串&#x2F;</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#地址"><span class="toc-text">地址</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#数字"><span class="toc-text">数字</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#首行-步进"><span class="toc-text">首行~步进</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#-3"><span class="toc-text">$</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#正则表达式"><span class="toc-text">&#x2F;正则表达式&#x2F;</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#c正则表达式c"><span class="toc-text">\c正则表达式c</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#GNU-sed-还支持一些特殊的2地址形式"><span class="toc-text">GNU sed 还支持一些特殊的2地址形式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#0-地址2"><span class="toc-text">0,地址2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#地址1-N"><span class="toc-text">地址1,+N</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#地址1-N-1"><span class="toc-text">地址1,~N</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#正则表达式-1"><span class="toc-text">正则表达式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#参考文献"><span class="toc-text">参考文献</span></a></li></ol>
    </div>
  </section>


            
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
      
    

  
</aside>

<footer id="footer" class="clearfix">
  
  
    <div class="social-wrapper">
      
        
          <a href="/atom.xml"
            class="social fas fa-rss flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="mailto:fkxxyz@163.com"
            class="social fas fa-envelope flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="https://github.com/fkxxyz"
            class="social fab fa-github flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="https://ti.qq.com/open_qq/index2.html?url=mqqapi%3a%2f%2fuserprofile%2ffriend_profile_card%3fsrc_type%3dweb%26version%3d1.0%26source%3d2%26uin%3d396519827"
            class="social fab fa-qq flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
    </div>
  
  <br>
  <div><p>博客内容遵循 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener">署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</a></p>
</div>
  <div>
    本站使用
    <a href="https://xaoxuu.com/wiki/material-x/" target="_blank" class="codename">Material X</a>
    作为主题
    
      ，
      总访问量为
      <span id="busuanzi_value_site_pv"><i class="fas fa-spinner fa-spin fa-fw" aria-hidden="true"></i></span>
      次
    
    。
  </div>
</footer>
<script>setLoadingBarProgress(80);</script>


      <script>setLoadingBarProgress(60);</script>
    </div>
    <a class="s-top fas fa-arrow-up fa-fw" href='javascript:void(0)'></a>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>

  <script>
    var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
    var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
    var ALGOLIA_API_KEY = "";
    var ALGOLIA_APP_ID = "";
    var ALGOLIA_INDEX_NAME = "";
    var AZURE_SERVICE_NAME = "";
    var AZURE_INDEX_NAME = "";
    var AZURE_QUERY_KEY = "";
    var BAIDU_API_ID = "";
    var SEARCH_SERVICE = "hexo" || "hexo";
    var ROOT = "/"||"/";
    if(!ROOT.endsWith('/'))ROOT += '/';
  </script>

<script src="//instant.page/1.2.2" type="module" integrity="sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU"></script>




  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-busuanzi@2.3/js/busuanzi.pure.mini.js"></script>




  
  
  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>
    <script type="text/javascript">
      $(function(){
        if ('') {
          $('').backstretch(
          ["/img/background.jpg"],
          {
            duration: "6000",
            fade: "2500"
          });
        } else {
          $.backstretch(
          ["/img/background.jpg"],
          {
            duration: "6000",
            fade: "2500"
          });
        }
      });
    </script>
  











  
<script src="/js/app.js"></script>



  
<script src="/js/search.js"></script>





<!-- 复制 -->
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  let COPY_SUCCESS = "复制成功";
  let COPY_FAILURE = "复制失败";
  /*页面载入完成后，创建复制按钮*/
  !function (e, t, a) {
    /* code */
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '  <i class="fa fa-copy"></i><span>复制</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });

      clipboard.on('success', function(e) {
        //您可以加入成功提示
        console.info('Action:', e.action);
        console.info('Text:', e.text);
        console.info('Trigger:', e.trigger);
        success_prompt(COPY_SUCCESS);
        e.clearSelection();
      });
      clipboard.on('error', function(e) {
        //您可以加入失败提示
        console.error('Action:', e.action);
        console.error('Trigger:', e.trigger);
        fail_prompt(COPY_FAILURE);
      });
    }
    initCopyCode();

  }(window, document);

  /**
   * 弹出式提示框，默认1.5秒自动消失
   * @param message 提示信息
   * @param style 提示样式，有alert-success、alert-danger、alert-warning、alert-info
   * @param time 消失时间
   */
  var prompt = function (message, style, time)
  {
      style = (style === undefined) ? 'alert-success' : style;
      time = (time === undefined) ? 1500 : time*1000;
      $('<div>')
          .appendTo('body')
          .addClass('alert ' + style)
          .html(message)
          .show()
          .delay(time)
          .fadeOut();
  };

  // 成功提示
  var success_prompt = function(message, time)
  {
      prompt(message, 'alert-success', time);
  };

  // 失败提示
  var fail_prompt = function(message, time)
  {
      prompt(message, 'alert-danger', time);
  };

  // 提醒
  var warning_prompt = function(message, time)
  {
      prompt(message, 'alert-warning', time);
  };

  // 信息提示
  var info_prompt = function(message, time)
  {
      prompt(message, 'alert-info', time);
  };

</script>


<!-- fancybox -->
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
<script>
  let LAZY_LOAD_IMAGE = "";
  $(".article-entry").find("fancybox").find("img").each(function () {
      var element = document.createElement("a");
      $(element).attr("data-fancybox", "gallery");
      $(element).attr("href", $(this).attr("src"));
      /* 图片采用懒加载处理时,
       * 一般图片标签内会有个属性名来存放图片的真实地址，比如 data-original,
       * 那么此处将原本的属性名src替换为对应属性名data-original,
       * 修改如下
       */
       if (LAZY_LOAD_IMAGE) {
         $(element).attr("href", $(this).attr("data-original"));
       }
      $(this).wrap(element);
  });
</script>





  <script>setLoadingBarProgress(100);</script>
</body>
</html>
